package com.easefun.polyv.livecommon.module.utils;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.easefun.polyv.R;
import com.easefun.polyv.livecommon.module.utils.rotaion.PLVOrientationManager;
import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.plv.thirdpart.blankj.utilcode.util.ScreenUtils;

import org.jetbrains.annotations.NotNull;

public class BackDialog extends CenterPopupView {
    private String title;
    private String msg;
    private String cancel;
    private String confirm;
    private OnConfirmListener onClickListener;

    public BackDialog(@NonNull @NotNull Context context) {
        super(context);
    }

    public BackDialog(@NonNull @NotNull Context context, String title, String msg, String confirm, String cancel, OnConfirmListener onClickListener) {
        super(context);
        this.msg = msg;
        this.title = title;
        this.cancel = cancel;
        this.confirm = confirm;
        this.onClickListener = onClickListener;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_back;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        TextView tvTitle = findViewById(R.id.tvTitle);
        TextView tvMsg = findViewById(R.id.tvMsg);
        tvTitle.setText(title);
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
            tvMsg.getPaint().setFakeBoldText(true);
            tvMsg.setTextSize(18f);
            tvMsg.setTextColor(Color.parseColor("#333333"));
        } else {
            tvMsg.setTextSize(14f);
            tvTitle.setVisibility(View.VISIBLE);
            tvMsg.getPaint().setFakeBoldText(false);
            tvMsg.setTextColor(Color.parseColor("#5A5A5A"));
        }
        TextView tvConfirm = findViewById(R.id.tvConfirm);
        tvConfirm.setText(confirm);
        tvConfirm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                onClickListener.onConfirm();
            }
        });
        View vLine = findViewById(R.id.vLine);
        TextView tvCancel = findViewById(R.id.tvCancel);
        tvCancel.setText(cancel);
        if (TextUtils.isEmpty(cancel)) {
            tvCancel.setVisibility(View.GONE);
            vLine.setVisibility(View.GONE);
        } else {
            tvCancel.setVisibility(View.VISIBLE);
            vLine.setVisibility(View.VISIBLE);
        }
        tvCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                PLVOrientationManager.getInstance().unlockOrientation();
            }
        });
    }

    @Override
    protected int getMaxWidth() {
        return ScreenUtils.getScreenWidth();
    }
}
